package com.teach.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.TreeMap;

/**
 * 功能描述：
 *
 * @author 祝枝山
 * @create 2022-04-20 23:53
 */
@Slf4j
@ControllerAdvice
//@RestControllerAdvice
public class CatchException {

    public static void main(String[] args) {
        TreeMap<String, Integer> stringIntegerTreeMap = new TreeMap<>();
        stringIntegerTreeMap.put("b", 1);
        stringIntegerTreeMap.put("a", 1);

        System.out.println(stringIntegerTreeMap.keySet());
    }

    @ExceptionHandler
    @ResponseBody
    public ReturnResult catchException(Exception exception){

        String clazzName = exception.getClass().getName();
        // 输出日志
        log.info("全局异常处理---" + clazzName);
        // 判断是否为自定义的异常
//        if (exception instanceof PinXiXiException){
//            return ResultInfo.error(250, ((PinXiXiException) exception).getMessage());
//        }

        exception.printStackTrace();

        return ReturnResult.error(250, "其他异常！" + exception.getMessage());
    }

}
